import useQuery from '@/api/query'
import { ref } from 'vue'
import xuanRanInformation from '@/utils/xuanRanMyInformation'

// 获取个人信息
export const getMyInformation = async () => {
  const res = ref()
  try{
    res.value = await useQuery.get('https://big-event-vue-api-t.itheima.net/my/userinfo')
  }
  catch (err){
    alert(err.data.message)
  }
  // console.log('res',res)
  const obj = res.value.data.data
  // console.log('obj',obj)
  return obj
}

// 更新头像
export const updateMyImg = async (avatar) => {
  await useQuery.patch('https://big-event-vue-api-t.itheima.net/my/update/avatar',{
    avatar,
  })
  return await xuanRanInformation()
}

// 更新密码
export const updateMyPassword = async (old_pwd,new_pwd,re_pwd) => {
  try{
    await useQuery.patch('https://big-event-vue-api-t.itheima.net/my/updatepwd',{
    old_pwd,
    new_pwd,
    re_pwd
    })
  console.log('密码成功修改')
  }
  catch (err) {
    alert(err.data.message)
  }
  return await xuanRanInformation()
}

// 更新个人资料
export const updateMyInformation = async (id,nickname,email) => {
  try{
    await useQuery.put('https://big-event-vue-api-t.itheima.net/my/userinfo',{
    id,
    nickname,
    email
  })
  }
  catch (err){
    alert(err.data.message)
  }
  return await xuanRanInformation()
}
